package com.meituan.catering.management.shop.biz.model.converter;

import com.meituan.catering.management.common.model.api.http.AuditingHttpModel;
import com.meituan.catering.management.common.model.api.http.ContactHttpModel;
import com.meituan.catering.management.common.model.biz.AuditingBO;
import com.meituan.catering.management.common.model.biz.PageBO;
import com.meituan.catering.management.shop.api.http.model.dto.ShopPageHttpDTO;
import com.meituan.catering.management.shop.biz.model.ShopBO;
import org.h2.mvstore.Page;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Believe
 * Date 2022/5/17 9:43
 */
public class ShopPageHttpDTOConverter {
    public static ShopPageHttpDTO toShopPageHttpResponse(PageBO<ShopBO> pageBO){
        ShopPageHttpDTO shopPageHttpDTO = new ShopPageHttpDTO();
        shopPageHttpDTO.setPageSize(pageBO.getPageSize());
        shopPageHttpDTO.setTotalPageCount(pageBO.getTotalPageCount());
        shopPageHttpDTO.setTotalCount(pageBO.getTotalCount());
        shopPageHttpDTO.setPageIndex(pageBO.getPageIndex());
        shopPageHttpDTO.setRecords(toRecord(pageBO.getRecords()));
        return shopPageHttpDTO;
    }

    private static List<ShopPageHttpDTO.Record> toRecord(List<ShopBO> list){
        List<ShopPageHttpDTO.Record> recordList = new ArrayList<>();
        for (ShopBO shopBO : list) {
            ShopPageHttpDTO.Record record = new ShopPageHttpDTO.Record();
            record.setId(shopBO.getId());
            record.setTenantId(shopBO.getTenantId());
            record.setVersion(shopBO.getVersion());
            record.setBusinessNo(shopBO.getBusinessNo());
            record.setName(shopBO.getName());
            record.setManagementType(shopBO.getManagementType());
            record.setBusinessType(shopBO.getBusinessType());
            record.setOpeningHours(toOpeningHours(shopBO.getOpenTime(), shopBO.getCloseTime()));
            record.setBusinessArea(shopBO.getBusinessArea());
            record.setComment(shopBO.getComment());
            record.setEnabled(shopBO.getEnabled());
            record.setContact(toContactHttpModel(shopBO));
            record.setAuditing(toAuditingHttpModel(shopBO.getAuditing()));
            recordList.add(record);
        }
        return recordList;
    }

    private static AuditingHttpModel toAuditingHttpModel(AuditingBO auditing) {
        AuditingHttpModel auditingHttpModel = new AuditingHttpModel();
        auditingHttpModel.setCreatedBy(auditing.getCreatedBy());
        auditingHttpModel.setCreatedAt(auditing.getCreatedAt());
        auditingHttpModel.setLastModifiedBy(auditing.getLastModifiedBy());
        auditingHttpModel.setLastModifiedAt(auditing.getLastModifiedAt());
        return auditingHttpModel;
    }

    private static ContactHttpModel toContactHttpModel(ShopBO shopBO) {
        ContactHttpModel contactHttpModel = new ContactHttpModel();
        contactHttpModel.setTelephone(shopBO.getTelephone());
        contactHttpModel.setCellphone(shopBO.getCellphone());
        contactHttpModel.setName(shopBO.getContactName());
        contactHttpModel.setAddress(shopBO.getContactAddress());
        return contactHttpModel;
    }

    public static ShopPageHttpDTO.Record.OpeningHoursTimeRange toOpeningHours(String openTime, String closeTime){
        ShopPageHttpDTO.Record.OpeningHoursTimeRange openingHoursTimeRange = new ShopPageHttpDTO.Record.OpeningHoursTimeRange();
        openingHoursTimeRange.setOpenTime(openTime);
        openingHoursTimeRange.setCloseTime(closeTime);
        return openingHoursTimeRange;
    }

}
